LED Display

by Mike Bradbury (mike@karemi.fsnet.co.uk)

Using a single bitmap to display two LED states, with a binary counter for free!

Home

LB Browser

4.03 Review

LED Display

Sprite Byte Masks

API Corner

Ini Files

Registration Keys

Eddie Fonts

Dragging a Popup

Random Audio Player

Eddie

Submissions

Index


The LB conforum now has a hardware forum, which has seen an increase in interest and enquiries relating to hardware interfacing and simulation of light emitting diodes (leds). This is my main field of interest and many of my projects utilise leds of varying colours.

Here I attempt to show just one method of displaying a circular led in both the on and off states, using one bitmap. A bas file and the bitmap file are both included in the newsletter download archive in a folder named LedsMBradbury. Copy the bas file to any folder of your choice and make sure the bitmap is copied to a bmp folder (or edit the path in the loadbmp command.

The bitmap is 17pixels wide and 30pixels in height and contains a dark green image of a led above a light green image. The bitmap was created in a popular image editor, so any colour led can be created to suit your application. The background colour of the bitmap should match the fill colour chosen for the graphicbox, in this case lightgray so that only the circular led is seen. The graphicbox in which the bitmap is drawn is 15pixels high and 136pixels wide, which allows for eight appearances of either the top half or bottom half of the bitmap to be drawn. Notice that the graphicbox border has has been removed, permitting the whole graphicbox area to be used. If the y coordinate for the bitmap in the graphicbox is zero, then the dark green image (led off) is displayed or if y coordinate is -15 then the bottom half of the bitmap, light green (led on) is displayed. The top half of the bitmap is then of course invisible, outside the graphicbox. The x coordinate is incremented in a loop, allowing each of the eight leds to be drawn side by side.

The same method can be used for rectangular leds if preferred, eliminating the need to bother about background colours. Drawing a realistic representation of a led being the most difficult part of creating an on screen simulation.

This demo also serves an additional function, being a demonstration of binary counting over the range 0 to 255, providing a real life purpose to the use of simulated leds. The leds are arranged to a convention where it is usual to show the least significant bit (lsb) to the right and the most significant bit to the left (msb). Thus the binary digit values appear as 128,64,32,16,8,4,2,1 from the left.

For anyone who does not understand binary, simply run the program which has start/stop control and stop the count at various stages. Add the bit value for each led which is illuminated (on) and the sum should equal the decimal count value shown in the statictext control #w.st . A 1 in the binary number corresponds with a led in the same position being on and a 0 with a led in the same position being off.

Demo - requires included bitmap


'Tested with LB4.03 & winXP/SP2

nomainwin
global START,count,yON,yOFF,LED$
LED$="greenLED"
loadbmp LED$, "bmp\greenLED.bmp"
yON=-15
yOFF=0
BackgroundColor$="lightgray"
ForegroundColor$="white"
WindowWidth = 300 : WindowHeight = 150
button #w.st, "Start",control, ul, 50,85,50,22
statictext #w.st0, "Decimal:",10,5,60,15
statictext #w.st1, "0",75,5,100,15
statictext #w.st2, "128(msb)";space$(3);"(lsb)1",7,60,140,20
statictext #w.st3, "Binary:",10,20,60,15
statictext #w.st4, "00000000",75,20,80,15
stylebits #w.gb, 0,_WS_BORDER,0,0
graphicbox #w.gb, 10, 40, 136, 15
stylebits #w, 0,_WS_VISIBLE,_WS_EX_TOPMOST,0
open "Bitmap LEDs & Binary Counter" for window_nf as #w
#w, "trapclose closeWindow"
#w, "font courier_new 10"
#w.gb, "down;size 15;fill lightgray"
'draw leds, all off
for n=1 to 8
    x=(n-1)*17  'led bmp is 17px wide
    #w.gb, "drawbmp ";LED$;" ";x;" ";yOFF
next n
#w.gb, "flush"
#w, "show"
wait

sub control h$
    START=1-START
    if START then
       timer 100, intervalCount
       #w.st, "Stop"
       else
       timer 0
       #w.st, "Start"
    end if
end sub

sub intervalCount
    count=count+1
    #w.st1, count
    bin$=DecBin$(count,yON,LED$)
    #w.st4, bin$
    if count=255 then count=0
end sub

sub closeWindow h$
    timer 0
    unloadbmp "greenLED"
    close #w
    end
end sub

function DecBin$(decNum,y,led$) 'convert count to binary
    bin$=""
    for x=7 to 0 step -1
        b = 2^x
        if b > decNum then
            bin$ = bin$+"0"
        else
            decNum = decNum-b
            bin$ = bin$+"1"
        end if
    next
    DecBin$ = bin$
    'draw leds on or off
    for b=1 to 8
        #w.gb, "drawbmp ";led$;" ";(b-1)*17;" "; val(mid$(bin$,b,1))*y
    next b
end function


Home

LB Browser

4.03 Review

LED Display

Sprite Byte Masks

API Corner

Ini Files

Registration Keys

Eddie Fonts

Dragging a Popup

Random Audio Player

Eddie

Submissions

Index